package com.example.demo; /**
 * 文件名：CharsetCodeTest.java
 * 功能描述：文件字符编码测试
 */

import lombok.Data;

import java.io.*;
import java.util.List;

@Data
public class CharsetCodeTest {

    public String getFileCharsetName(String fileName) throws IOException {
        InputStream inputStream = new FileInputStream(fileName);
        byte[] head = new byte[3];
        inputStream.read(head);

        String charsetName = "GBK";//或GB2312，即ANSI
        if (head[0] == -1 && head[1] == -2 ) //0xFFFE
            charsetName = "UTF-16";
        else if (head[0] == -2 && head[1] == -1 ) //0xFEFF
            charsetName = "Unicode";//包含两种编码格式：UCS2-Big-Endian和UCS2-Little-Endian
        else if(head[0]==-27 && head[1]==-101 && head[2] ==-98)
            charsetName = "UTF-8"; //UTF-8(不含BOM)
        else if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
            charsetName = "UTF-8"; //UTF-8-BOM

        inputStream.close();

        //System.out.println(code);
        return charsetName;
    }

    public void writeJsFileFromJson(int pressoffset,int time,String jsFilePath,SkyStudio skyStudio) {
        int pressoffset2 = pressoffset*2;
        // 写入 JS 文件
        try(FileWriter writer = new FileWriter(jsFilePath);) {
            writer.write("var time=" + time + ";\n");
            writer.write("var time2=" + time*2 + ";\n");
            writer.write("var time4=" + time*4 + ";\n");
            writer.write("var pressoffset=" + pressoffset + ";\n");
            writer.write("var pressoffset2=" + pressoffset2 + ";\n");
            writer.write("var stop=1;\n");
            writer.write("var speedControl=1;\n");
            writer.write("var zuobiaoPath = \"/sdcard/Download/SkyMsToJs/zuobiao.txt\";\n");
            writer.write("\n");
//            writer.write("if (files.exists(zuobiaoPath)) {\n");
//            writer.write("    eval(files.read(zuobiaoPath));\n");
//            writer.write("} else {\n");
            ///手机按键
//            writer.write("setScreenMetrics(1080, 2340);\n");
//            writer.write("var x=[780,975,1170,1365,1560];\n");
//            writer.write("var y=[215,410,605];\n");
            ///模拟器按键
//            writer.write("setScreenMetrics(920, 1472);\n");
//            writer.write("var x=[520,630,735,845,950];\n");
//            writer.write("var y=[125,230,335];\n");
            ///pad按键
            writer.write("setScreenMetrics(1840, 2944);\n");
            writer.write("var x=[1070,1270,1470,1670,1870];\n");
            writer.write("var y=[245,440,635];\n");
//            writer.write("}\n");
            writer.write("\n");
            writer.write("function ran() {\n");
            writer.write("    return Math.random() * pressoffset2 - pressoffset;\n");
            writer.write("}\n");
            writer.write("\n");
            writer.write("function key0() { press(x[0] + ran(), y[0] + ran(),1); }\n");
            writer.write("function key1() { press(x[1] + ran(), y[0] + ran(),1); }\n");
            writer.write("function key2() { press(x[2] + ran(), y[0] + ran(),1); }\n");
            writer.write("function key3() { press(x[3] + ran(), y[0] + ran(),1); }\n");
            writer.write("function key4() { press(x[4] + ran(), y[0] + ran(),1); }\n");
            writer.write("function key5() { press(x[0] + ran(), y[1] + ran(),1); }\n");
            writer.write("function key6() { press(x[1] + ran(), y[1] + ran(),1); }\n");
            writer.write("function key7() { press(x[2] + ran(), y[1] + ran(),1); }\n");
            writer.write("function key8() { press(x[3] + ran(), y[1] + ran(),1); }\n");
            writer.write("function key9() { press(x[4] + ran(), y[1] + ran(),1); }\n");
            writer.write("function key10() { press(x[0] + ran(), y[2] + ran(),1); }\n");
            writer.write("function key11() { press(x[1] + ran(), y[2] + ran(),1); }\n");
            writer.write("function key12() { press(x[2] + ran(), y[2] + ran(),1); }\n");
            writer.write("function key13() { press(x[3] + ran(), y[2] + ran(),1); }\n");
            writer.write("function key14() { press(x[4] + ran(), y[2] + ran(),1); }\n");
            writer.write("\n");
            writer.write("var window = floaty.window(<frame><vertical><button id=\"btn\" text='开始'/><horizontal><button id=\"speedLow\" text='减速' w=\"80\"/><button id=\"speedHigh\" text='加速' w=\"80\"/></horizontal><horizontal><button id=\"speed\" text='1.0x' w=\"80\"/><button id=\"stop\" text='停止' w=\"80\"/></horizontal></vertical></frame>);\n");
            writer.write("\n");
            writer.write("window.btn.click(() => {\n");
            writer.write("    if (stop) {\n");
            writer.write("        stop = 0;\n");
            writer.write("        window.btn.setText('开始');\n");
            writer.write("    } else {\n");
            writer.write("        stop = 1;\n");
            writer.write("        window.btn.setText('暂停');\n");
            writer.write("    }\n");
            writer.write("});\n");
            writer.write("\n");
            writer.write("window.speedHigh.click(() => {\n");
            writer.write("    speedControl=(speedControl*10+1)/10;\n");
            writer.write("    window.speed.setText(speedControl+'x');\n");
            writer.write("});\n");
            writer.write("\n");
            writer.write("window.speedLow.click(() => {\n");
            writer.write("    if(speedControl<=0.1){return;}speedControl=(speedControl*10-1)/10;\n");
            writer.write("    window.speed.setText(speedControl+'x');\n");
            writer.write("});\n");
            writer.write("\n");
            writer.write("window.speed.click(() => {\n");
            writer.write("    speedControl = 1;\n");
            writer.write("    window.speed.setText('1.0x');\n");
            writer.write("});\n");
            writer.write("\n");
            writer.write("window.stop.click(() => {\n");
            writer.write("    engines.all().forEach(engine => engine.forceStop());\n");
            writer.write("});\n");
            writer.write("\n");
            writer.write("function start() {\n");
            writer.write("    while (stop) { sleep(100); }\n");
            writer.write("}\n");
            writer.write("\n");
            writer.write("function t1() {\n");
            writer.write("    while (stop) { sleep(100) }\n");
            writer.write("    sleep(time / speedControl);\n");
            writer.write("}\n");
            writer.write("\n");
            writer.write("function t2() {\n");
            writer.write("    while (stop) { sleep(100) }\n");
            writer.write("    sleep(time2 / speedControl);\n");
            writer.write("}\n");
            writer.write("\n");
            writer.write("function t4() {\n");
            writer.write("    while (stop) { sleep(100) }\n");
            writer.write("    sleep(time4 / speedControl);\n");
            writer.write("}\n");
            writer.write("\n");
            writer.write("start();\n");
//            writer.write("a4(); t2(); e5(); t2(); c6(); t2();\n");
            writerLastStudio(writer,skyStudio,time);
            System.out.println("Script written to " + jsFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void writerLastStudio(FileWriter writer,SkyStudio skyStudio,int time) throws IOException{
        List<SongNotes> songNotes = skyStudio.getSongNotes();
        int LastSong = -1;
        for (SongNotes s1:songNotes){
            if(LastSong == -1){
                LastSong = s1.getTime();
                WriterTime(writer,LastSong,time);
            }
            else {
                if(s1.getTime()-LastSong > 0){
                    WriterTime(writer,s1.getTime()-LastSong,time);
                }
            }
            String funcKey = s1.getKey().substring(1).toLowerCase();
            LastSong = s1.getTime();
            writer.write(funcKey+ "();");
        }
    }

    private void WriterTime(FileWriter writer,int TimeSec,int time)throws IOException{
        //TimeSec 间隔时间长度
        while(TimeSec/(4*time)>=1){
            TimeSec = TimeSec - 4*time;
            writer.write("t4();");
        }
        while(TimeSec/(2*time)>=1){
            TimeSec = TimeSec - 2*time;
            writer.write("t2();");
        }
        while(TimeSec/(time)>=1){
            TimeSec = TimeSec - time;
            writer.write("t1();");
        }
        if(TimeSec>0){
            System.err.println(TimeSec);
            writer.write("t1();");
        }
    }
}